package cn.edu.cuit.aumalls.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.cuit.aumalls.entity.CartGoods;
import cn.edu.cuit.aumalls.entity.OrderRecipients;
import cn.edu.cuit.aumalls.service.CartIntentService;

@Controller
public class CartIndentController {
	
	@Autowired
	CartIntentService cartIntentService;
	
	@RequestMapping("/showCartIndent")
	public String showCartIndent(Model model) {
		List<OrderRecipients> orderRecipientlist = cartIntentService.showCartIntent(1);
		List<CartGoods> cartGoodslist = cartIntentService.showCartGoods();
		float discount = cartIntentService.showDiscount();
		float totalPrice = cartIntentService.showTotalPrice();
		double goodsDuty = cartIntentService.showDuty();
		
		model.addAttribute("orderRecipientlist", orderRecipientlist);
		model.addAttribute("cartGoodslist",cartGoodslist);
		model.addAttribute("discount", discount);
		model.addAttribute("totalPrice", totalPrice);
		model.addAttribute("goodsDuty", goodsDuty);
		return "Cart-indent";
	}
	
	@RequestMapping("/addRecipient")
	public String addRecipient(@RequestParam(value="recipients_name") String recipients_name,
			@RequestParam(value="recipients_address_first")String recipients_address_first,
			@RequestParam(value="recipients_address_second")String recipients_address_second,
			@RequestParam(value="recipients_phone")String recipients_phone,@RequestParam(value="checkedbox",required=false)String recipients_checked) {
		OrderRecipients orderRecipients = new OrderRecipients();
		
		//System.out.println(recipients_checked);
		
		
		
		orderRecipients.setRecipients_name(recipients_name);
		orderRecipients.setRecipients_address(recipients_address_first+recipients_address_second);
		orderRecipients.setRecipients_phone(recipients_phone);
		orderRecipients.setRecipients_user(1);
		
		if("recipients_checked".equals(recipients_checked)) {
			cartIntentService.updateChecked();
			orderRecipients.setRecipients_checked(1);
		}
		else {
			orderRecipients.setRecipients_checked(0);
		}
		
		System.out.println(orderRecipients);
		
		cartIntentService.addRecipient(orderRecipients);
		return "redirect:/showCartIndent";
	}
	
	@RequestMapping("/submitOrder")
	public String submitOrder(@RequestParam("totalprice") double totalprice) {
		cartIntentService.submit(totalprice,1);
		return "redirect:/showPay";
	}
}
